package cn.moningxi.service.impl;

import cn.moningxi.bean.CustomerInfomation;
import cn.moningxi.bean.OrderDetail;
import cn.moningxi.bean.OrderMaster;
import cn.moningxi.controller.OrderDetailController;
import cn.moningxi.mapper.*;
import cn.moningxi.service.OrderMasterService;
import cn.moningxi.util.CustomIdGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

@Service
public class OrderMasterServiceImpl implements OrderMasterService {
    @Autowired
    OrderMasterMapper orderMasterMapper;
    @Autowired
    OrderDetailMapper orderDetailMapper;
    @Autowired
    CustomerInfomationMapper customerInfomationMapper;
    @Autowired
    GGoodsReportMapper gGoodsReportMapper;

    @Override
    public List<OrderMaster> getOrder(String customerId) {
        return orderMasterMapper.getOrder(customerId);
    }

    @Override
    @Transactional
    public void addDetail(OrderDetail orderDetail, String uid) {
        //设置订单细节表id
        orderDetail.setOrderId(new CustomIdGenerator().nextId(OrderDetailController.class));
        orderDetail.setModifiedTime(LocalDateTime.now());
        orderDetailMapper.addDetail(orderDetail);
        OrderMaster orderMaster = new OrderMaster();
        //设置订单数据
        List<OrderMaster> orderMasterList = orderMasterMapper.queryNoPayByCid(uid);
        for (OrderMaster master : orderMasterList) {
            //商品数据已在订单里
            if (master.getOrderDetail().getGoodsId().equals(orderDetail.getGoodsId())) {
                orderDetailMapper.updateCnt(master.getOrderDetail(), orderDetail.getGoodsCnt());
                return;
            }
        }
        orderMaster.setOrderId(orderDetail.getOrderId());
        orderMaster.setOrderSn(UUID.randomUUID().toString());
        orderMaster.setCustomerId(uid);
        orderMaster.setOrderMoney(orderDetail.getGoodsCnt() * orderDetail.getGoodsPrice());
        orderMaster.setOrderStatus("0");
        orderMaster.setCreateTime(LocalDateTime.now());
        orderMaster.setModifiedTime(LocalDateTime.now());
        orderMasterMapper.addOrder(orderMaster);
    }

    @Override
    public List<OrderMaster> queryNoPayByCid(String customerId) {
        return orderMasterMapper.queryNoPayByCid(customerId);
    }

    @Transactional
    @Override
    public void unifiedOrder(CustomerInfomation onlineUser) {
        List<OrderMaster> orderMasterList = orderMasterMapper.queryNoPayByCid(onlineUser.getCustomerId());
        for (OrderMaster master : orderMasterList) {
            master.setPayTime(LocalDateTime.now());
            master.setModifiedTime(LocalDateTime.now());
            master.setOrderMoney(master.getOrderDetail().getGoodsCnt() * master.getOrderDetail().getGoodsPrice());
            master.setPayMoney(master.getOrderDetail().getGoodsCnt() * master.getOrderDetail().getGoodsPrice());
            master.setOrderStatus("1");
            orderMasterMapper.unifiedOrder(master);
            //更新商品销量
            gGoodsReportMapper.updateAmount(master.getOrderDetail().getGoodsId(), master.getOrderDetail().getGoodsCnt());
        }
        //更新账号余额
        customerInfomationMapper.updateBalance(onlineUser);
    }

    @Transactional
    @Override
    public void deleteOrderById(Long orderId) {
        orderDetailMapper.deleteOrderById(orderId);
        orderMasterMapper.deleteOrderById(orderId);
    }

    @Override
    public List<OrderMaster> selectAllOrderInfomation() {
        return orderMasterMapper.selectAllOrderInfomation();
    }

    @Override
    public OrderMaster selectOrderInfoById(Integer orderId) {
        return orderMasterMapper.selectOrderInfoById(orderId);
    }
}
